package com.zst.licesingservice.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zst.licesingservice.mapper.LicenseMapper;
import com.zst.licesingservice.model.License;
import com.zst.licesingservice.model.Organization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LicenseService {
    @Autowired
    private LicenseMapper mapper;

    @HystrixCommand(
            fallbackMethod= "fallback",
            threadPoolKey = "getLicense",
            threadPoolProperties = {
              @HystrixProperty(name = "coreSize", value = "30"),
              @HystrixProperty(name = "maxQueueSize", value = "10"),

            },
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "3"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "75"),
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "7000"),
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "15000"),
                    @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "5"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
            }
    )
    public License getLicense(String organizationId, String licenseId){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return fallback(organizationId, licenseId);
//        return mapper.findByOrganizationIdAndLicenseId(organizationId, licenseId);
    }

    public License getLicense(String organizationId, String licenseId, String clientType){
        License license = mapper.findByOrganizationIdAndLicenseId(organizationId, licenseId);

//        Organization org = retriveOrgInfo(organizationId, clientType);


        return null;
    }

    public License fallback(String organizationId, String licenseId){
        License result = new License();
        result.setProductName("CirCling");
        return result;
    }
}
